package tps12.axwax.client.widgets;

import tps12.axwax.client.PuzzleClientImpl;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Composite widget for selecting a puzzle.
 * 
 * @author Travis
 *
 */
public class UploadPuzzle extends Composite {
	/**
	 * Constructs a puzzle upload widget.
	 */
	public UploadPuzzle() {
		final VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		
	    // Create a FormPanel and point it at a service.
	    final FormPanel form = new FormPanel();
	    form.setAction(
	    		new PuzzleClientImpl(new PuzzleClientImpl.GwtRequestBuilderFactory())
	    		.baseUrl + "/");

	    // Because we're going to add a FileUpload widget, we'll need to set the
	    // form to use the POST method, and multipart MIME encoding.
	    form.setEncoding(FormPanel.ENCODING_MULTIPART);
	    form.setMethod(FormPanel.METHOD_POST);

	    // Create a panel to hold all of the form widgets.
	    HorizontalPanel widgets = new HorizontalPanel();
	    form.setWidget(widgets);

	    // Create a FileUpload widget.
	    FileUpload upload = new FileUpload();
	    upload.setName("puzzle");
	    widgets.add(upload);

	    // Add a 'submit' button.
	    widgets.add(new Button("Submit", new ClickHandler() {
	      public void onClick(ClickEvent event) {
	        form.submit();
	      }
	    }));

	    panel.add(form);
	}
}
